using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLThuChi.Repositories
{
	#region using
	using QLThuChi.Models;
	#endregion
	public class GiaiDoan_NhanCongRepository
	{
		public static List<GiaiDoan_NhanCong> SelectAll()
		{
			return DataContext.Instance.GiaiDoan_NhanCong.ToList();
		}

		public static GiaiDoan_NhanCong SelectByID(int manhanconggiaidoan)
		{
			return DataContext.Instance.GiaiDoan_NhanCong.FirstOrDefault(item => item.MaNhanCongGiaiDoan == manhanconggiaidoan );
		}

		public static bool Insert(GiaiDoan_NhanCong obj)
		{
			try
			{
				DataContext.Instance.GiaiDoan_NhanCong.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(int manhanconggiaidoan)
		{
			try
			{
				var delitem = DataContext.Instance.GiaiDoan_NhanCong.FirstOrDefault(item => item.MaNhanCongGiaiDoan == manhanconggiaidoan );
				DataContext.Instance.GiaiDoan_NhanCong.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

        public static bool DeleteByMaCongTrinhGiaiDoan(int macongtrinhgiaidoan)
        {
            try
            {
                var lstItem = GiaiDoan_NhanCongRepository.SelectByMaCongTrinhGiaiDoan(macongtrinhgiaidoan);
                for (int i = 0; i < lstItem.Count; i++)
                {
                    Delete(lstItem[i].MaNhanCongGiaiDoan);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<GiaiDoan_NhanCong> RetrieveByID(int manhanconggiaidoan)
		{
			return (from item in DataContext.Instance.GiaiDoan_NhanCong where  item.MaNhanCongGiaiDoan == manhanconggiaidoan  select item).ToList();
		}

		public static List<GiaiDoan_NhanCong> SelectByMaNhanCong(int manhancong)
		{
			var lstItem = (from item in DataContext.Instance.GiaiDoan_NhanCong where item.MaNhanCong == manhancong select item).ToList();
			return lstItem;
		}

		public static List<GiaiDoan_NhanCong> SelectByMaCongTrinhGiaiDoan(int macongtrinhgiaidoan)
		{
			var lstItem = (from item in DataContext.Instance.GiaiDoan_NhanCong where item.MaCongTrinhGiaiDoan == macongtrinhgiaidoan select item).ToList();
			return lstItem;
		}

        public static List<GiaiDoan_NhanCong> SelectByMaCongTrinhGiaiDoanNhanCong(int macongtrinhgiaidoan, int manhancong)
        {
            var lstItem = (from item in DataContext.Instance.GiaiDoan_NhanCong where item.MaCongTrinhGiaiDoan == macongtrinhgiaidoan && item.MaNhanCong == manhancong select item).ToList();
            return lstItem;
        }

	}
}